<template>
  <el-dialog
    :title="'查看权限'"
        :fullscreen="true"
    top="0"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">

      <el-form-item size="mini" label="授权">
        <el-tree
          :data="menuList"
     
          :props="menuListTreeProps"
          node-key="menuId"
          ref="menuListTree"
          :default-expand-all="true"
          show-checkbox>
        </el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>

    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        canEditor: false,
        menuList: [],
        menuListTreeProps: {
          label: 'name',
          children: 'children',
          disabled: this.disabledFn
        },
        dataForm: {
          id: 0,
          roleName: '',
          remark: ''
        },
        dataRule: {

        },
        tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
      }
    },
    methods: {
        disabledFn(){
            return true
        },

      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/menu/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.menuList = treeDataTranslate(data, 'menuId')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.$refs.menuListTree.setCheckedKeys([])
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/auth/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                var idx = data.menuIdList.indexOf(this.tempKey)
                if (idx !== -1) {
                  data.menuIdList.splice(idx, data.menuIdList.length - idx)
                }
                this.$refs.menuListTree.setCheckedKeys(data.menuIdList)
              }
              this.canEditor = true
            })
          }
        })
      }
     
      
    }
  }
</script>
<style>

.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {

    background-color: #3165d2;
    border-color: #e4e7ed;
    color: #313b50;
}

.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {

    background-color: #3165d2;
    border-color: #e4e7ed;
    color: #313b50;
}

</style>